<?php

namespace app\admin\controller;


class Dynamic extends Common {


	//新闻动态表
	private $table_admin = "";

	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();
		$this->table_admin = db('dynamic');
	}

	public function index(){
		$pageParam = ['query' =>[]];
		$where = [];
		$keyword = input('keyword');
		if( $keyword != ''){
			$where['title'] = array('like','%'.input('keyword').'%');
			$pageParam['query']['keyword'] = $keyword;
			$this->assign('keyword', $keyword);
		}
		$is_display = input('is_display');
		$this->assign('is_display', $is_display);
		if($is_display != ''){
			$where['is_display'] = input('is_display');
			$pageParam['query']['is_display'] = $is_display;

		}
		$category = input('category');
		$this->assign('category', $category);
		if($category != ''){
			$where['dc.id'] = input('category');
			$pageParam['query']['category'] = $category;

		}
		$category_result = db('dynamic_category')->select();
		$data = $this->table_admin->alias('dy')->join('dynamic_category dc','dy.category = dc.id')->field('dy.*,dc.category_name')->where($where)->order('order_by','desc')->paginate(10, false, $pageParam);
		$page = $data->render();
		$this->assign('page', $page);
		$this->assign('category_result', $category_result);
		$this->assign('data',$data);
		return 	$this->fetch();
	}

	/*
	 * 添加或修改
	 * */
	public function add(){
		if(request()->isAjax()) {
			$title = input('title');
			$order_by = input('order_by');
			$department = input('department');
			$file = request()->file('picture');
			$is_important = input('is_important');
			$is_display = input('is_display');
			$time = strtotime(input('time'));
			$link = input('link');
			$category = input('category');
			$content = input('content');
//			halt($file);
			if(!$title)
				$this->error('标题');
			$data['title'] = $title;
			$data['department'] = $department;
			$data['is_important'] = $is_important;
			$data['is_display'] = $is_display;
			$data['create_time'] = $time;
			$data['link'] = $link;
			$data['category'] = $category;
			$data['content'] = $content;
			$data['order_by'] = $order_by;

			if ($file) {
				$info = $file->move(ROOT_PATH . 'public/static/uploads');
				if ($info) {
					$data['picture'] = $info->getSaveName();
					if(input('id')){
						$id = input('id');
						$pic_value = $this->table_admin->where('id',$id)->value('picture');
						$pic = ROOT_PATH . 'public/static/uploads/'.$pic_value;
						if(file_exists($pic) && !empty($pic_value)){
							unlink($pic);
						}
					}
				} else {
					$this->success($file->getError());
				}
			} else {
				$data['picture'] = input('picture');
			}
			$id = input('id');
			if(!$id){
				//添加
				 $this->table_admin->insert($data);
			}else{
//				$data['id'] = input('id');
				//修改
				$id = input('id');
				$this->table_admin->where('id',$id)->update($data);
//				$sql = $this->table_admin->getLastSql();
//				$this->success($sql);
			}
			$this->success("操作成功!",url('dynamic/index'));
		}
		$category = $this->getCategory();
		$this->assign('category',$category);
		return $this->fetch();
	}

	public function edit(){
		$id = input('id');
		$data = $this->table_admin->where('id',$id)->find();
		$category = $this->getCategory();
		$this->assign('category',$category);
		$this->assign('data',$data);
		return $this->fetch();
	}

	public function delete(){
		$id = input("id");
		if(!$id) $this->error('参数有误');
		$da=$this->table_admin->where("id='$id'")->find();
		if(!$da) $this->error('数据不存在');
		$pic = ROOT_PATH . 'public/static/uploads/'.$da['picture'];
		if(!empty($da['picture'])){
			unlink($pic);
		}
		$res=$this->table_admin->where("id='$id'")->delete();
		if(!$res) $this->error('删除失败');
		$this->success("删除成功!",url('dynamic/li'));
	}
}